package site.zbox.tool.swing.xml.component;

import javax.swing.JComponent;
import javax.swing.JTextField;

import org.dom4j.Element;

import site.zbox.tool.lang.NumberUtils;
import site.zbox.tool.lang.StringUtils;
import site.zbox.tool.lang.Version;
import site.zbox.tool.swing.xml.IComponentParser;

@Version("2021.08.12")
public class TextFieldComponentParser implements IComponentParser {
    @Override
    public String name() {
        return "TextField";
    }

    @Override
    public JComponent parse(final Element element) {
        final JTextField label = new JTextField();
        label.setText(StringUtils.stripToEmpty(element.attributeValue("text")));
        final String editable = StringUtils.stripToEmpty(element.attributeValue("editable"));
        if (StringUtils.isNotEmpty(editable)) {
            label.setEditable("true".equals(editable));
        }
        final String columns = StringUtils.stripToEmpty(element.attributeValue("columns"));
        if (StringUtils.isNotEmpty(columns)) {
            label.setColumns(NumberUtils.toInt(columns));
        }
        return label;
    }
}
